package br.net.woodstock.tickettoride.api.core.utils;

import java.util.regex.Pattern;

import br.net.woodstock.rockframework.utils.ConditionUtils;

public abstract class FilterHelper {

	public static final String	ANY_PATTERN	= ".*";

	public static final String	BEGIN_CHAR	= "^";

	public static final String	START_CHAR	= "[";

	public static final String	END_CHAR	= "]";

	public static final String	START_GROUP	= "(";

	public static final String	END_GROUP	= ")";

	public static final String	SEPARATOR	= ":";

	public static final String	REPLACEMENT	= "";

	private FilterHelper() {
		//
	}

	public static Filter getStartWith(final String start) {
		if (ConditionUtils.isEmpty(start)) {
			return new Filter(Pattern.compile(FilterHelper.ANY_PATTERN));
		}
		StringBuilder builder = new StringBuilder();
		char[] array = start.toCharArray();

		builder.append(FilterHelper.BEGIN_CHAR);
		builder.append(FilterHelper.START_GROUP);
		for (char c : array) {
			if (Character.isLetter(c)) {
				builder.append(FilterHelper.START_CHAR);
				builder.append(Character.toUpperCase(c));
				builder.append(Character.toLowerCase(c));
				builder.append(FilterHelper.END_CHAR);
			} else if (Character.isDigit(c)) {
				builder.append(c);
			} else {
				builder.append(Pattern.quote(Character.toString(c)));
			}
		}
		builder.append(FilterHelper.END_GROUP);
		builder.append(FilterHelper.SEPARATOR);
		builder.append(FilterHelper.ANY_PATTERN);

		Pattern pattern = Pattern.compile(builder.toString());
		return new Filter(pattern);
	}
}
